//
//  LocationMapViewController.m
//  auditorias
//
//  Created by Mauro on 7/28/13.
//  Copyright (c) 2013 Antilope. All rights reserved.
//

#import "LocationMapViewController.h"
#import "KeyboardManager.h"
#import "MBProgressHUD.h"
#import "DataLoader.h"
#import "SharedMemory.h"
#import "ControlVariables.h"
#import "PointOfSell.h"
#import "User.h"

@interface LocationMapViewController ()

@end

@implementation LocationMapViewController

@synthesize mailTf;
@synthesize doButton;
@synthesize backgroundButton;
@synthesize scrollView;
@synthesize keyboardManager;
@synthesize mapView;

- (void) dealloc
{
    [mailTf release], mailTf = nil ;
    [doButton release], doButton = nil;
    [backgroundButton release], backgroundButton = nil;
    [scrollView release], scrollView = nil;
    [keyboardManager release], keyboardManager = nil;
    [mapView release], mapView = nil ;
    [_topBarView release];
    [super dealloc];
}

- (void)viewDidUnload
{
    self.mailTf = nil;
    self.doButton = nil;
    self.backgroundButton = nil;
    self.scrollView = nil;
    self.keyboardManager = nil;
    self.mapView = nil ;
    [super viewDidUnload];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.mapView.delegate = self;
    [TopBarViewController setTitleLabelOf:_topBarView text:kTopBarTitleLocationMap];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.keyboardManager = nil;
    [super viewWillDisappear:animated];
}


- (void)viewWillAppear:(BOOL)animated
{
    //[self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];
    
    
    self.keyboardManager = [[[KeyboardManager alloc] initWithDelegate:self.backgroundButton scrollView:self.scrollView elevation:20] autorelease];
    //make contentSize bigger than your scrollSize (you will need to figure out for your own use case)
    CGSize scrollContentSize = CGSizeMake(320, 345);
    self.scrollView.contentSize = scrollContentSize;
    
    //SharedMemory *mem = [SharedMemory sharedInstance];
    
    //[self.mapView addAnnotation:[self.mapView userLocation]];
    
    //Setting zoom and initial location
    CLLocationCoordinate2D zoomLocation = [[LocationListenerViewController getLastGoodLocation] coordinate];
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation,5*100*2, 5*100*2);
    MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
    [self.mapView setRegion:adjustedRegion animated:YES];
    [self showCurrentLocation];
}

- (IBAction)doButtonTapped:(id)sender
{
    [self backgroundTapped:sender];
    
    CLLocation* loc = [LocationListenerViewController getLastGoodLocation];
    if (loc==nil){
        [SharedMemory showError:@"" andText:location_no_location_cant_continue ];
        return;
    }
    NSString* pointName = self.mailTf.text;
    if ([pointName isEqualToString:@""]){
        [SharedMemory showError:@"" andText:location_no_point_of_sell_name_entered ];
        return;
    }
    SharedMemory* mem = [SharedMemory sharedInstance];
    PointOfSell* point = [[[PointOfSell alloc] initWithLocation:[loc coordinate] description:pointName] autorelease];
    [mem.pointsOfSell insertObject:point atIndex:0];
    mem.pointOfSellIndex = 0;
    [mem resetAuditTypesDone];
    
    [self.tabBarController setSelectedIndex:1];
    [self.navigationController popViewControllerAnimated:NO];
}


#pragma mark - Keyboard hiding


- (IBAction)backgroundTapped:(id)sender
{
	[self.mailTf resignFirstResponder];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	[textField resignFirstResponder];
    return YES;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    [super locationManager:manager didUpdateToLocation:newLocation fromLocation:oldLocation];
    CLLocation* loc = [LocationListenerViewController getLastGoodLocation];
    if (loc==nil)
        return;
    [self.mapView setCenterCoordinate:[loc coordinate] animated:YES];
    [self.mapView removeAnnotations:self.mapView.annotations];
    [self showCurrentLocation];
}

#pragma mark - Map delegate
/*
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    //[mapView setCenterCoordinate:[[userLocation location]coordinate] animated:YES];
    //[mapView setShowsUserLocation:YES];
}

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil; //for some reason this is only being returned when the view is pushed and the get
    //location method is called but not when the same location method is called
    //while the view already exists.
    return nil;
}*/


- (void)showCurrentLocation
{
    self.mapView.showsUserLocation = YES;
    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
    annotation.coordinate = [[LocationListenerViewController getLastGoodLocation] coordinate];
    annotation.title = @"Location";
    [self.mapView addAnnotation:annotation];
    [annotation release];
}

@end
